class TreeNode(object):
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right


class Solution(object):
    def getDirections(self, root, startValue, destValue):
        self.dist = ''

        def dfs(node):
            s_flag = False
            d_flag = False
            s_path = ''
            d_path = ''
            if node.left:
                ls_flag, ld_flag, ls_path, ld_path = dfs(node.left)
                s_flag |= ls_flag
                d_flag |= ld_flag
                if ls_flag:
                    s_path = ls_path + 'U'
                if ld_flag:
                    d_path = 'L' + ld_path
            if node.right:
                rs_flag, rd_flag, rs_path, rd_path = dfs(node.right)
                s_flag |= rs_flag
                d_flag |= rd_flag
                if rs_flag:
                    s_path = rs_path + 'U'
                if rd_flag:
                    d_path = 'R' + rd_path
            if s_path:
                if d_path:
                    if not self.dist:
                        self.dist = s_path + d_path
                else:
                    if node.val == destValue:
                        d_flag = True
                        self.dist = s_path
            else:
                if d_path:
                    if node.val == startValue:
                        s_flag = True
                        self.dist = d_path
                else:
                    if node.val == startValue:
                        s_flag = True
                    if node.val == destValue:
                        d_flag = True
            return s_flag, d_flag, s_path, d_path

        dfs(root)
        return self.dist

